﻿using MediatR;
using Microsoft.AspNetCore.Mvc;
using Production.ApplicationLib.Command;
using Production.ApplicationLib.Queries;
using Production.CommonLib.Constants;
using Production.CommonLib.Models;

namespace Production.WebApi.Controllers
{
    [ApiController]
    public class TechnologyController : ControllerBase
    {
        private readonly IMediator _mediator;
        private readonly ITechnologyQueries _technologyQueries;

        public TechnologyController(IMediator mediator,
            ITechnologyQueries technologyQueries)
        {
            _mediator = mediator;
            _technologyQueries = technologyQueries;
        }

        [HttpGet(ApiRoutes.Technology.QueryTechnologies)]
        public async Task<IActionResult> Get([FromQuery] TechnologySearchRequestDto model)
        {
            if (model.Page > 0 || model.Limit > 0)
            {
                var data = await _technologyQueries.QueryTechnologiesByPage(model);
                return Ok(data);
            }
            else
            {
                var data = await _technologyQueries.QueryTechnologies(model);
                return Ok(data);
            }
        }

        [HttpPost(ApiRoutes.Technology.CreateTechnology)]
        public async Task<IActionResult> Post([FromBody] CreateTechnologyCommand command)
        {
            var result = await _mediator.Send(command);
            return Ok(result);
        }

        [HttpGet(ApiRoutes.Technology.GetTechnology)]
        public async Task<IActionResult> Get(int id)
        {
            var result = await _technologyQueries.GetTechnology(id);
            return Ok(result);
        }

        [HttpPut(ApiRoutes.Technology.UpdateTechnology)]
        public async Task<IActionResult> Put(int id, UpdateTechnologyCommand command)
        {
            command.Id = id;
            var result = await _mediator.Send(command);
            return Ok(result);
        }
    }
}